﻿using System;
using System.Net.NetworkInformation;
using Citi.Helpers;
using Citi.Scheduler.Domain;
using System.Configuration;
using System.Collections.Generic;

namespace Citi.Scheduler.WebUi.Models
{
    public class MachineModel : ModelBase<MachineModel, Machine>
    {
        private string NetworkUser
        {
            get { return ConfigurationManager.AppSettings["NetworkUser"]; }
        }

        private string NetworkPassword
        {
            get { return ConfigurationManager.AppSettings["NetworkPassword"]; }
        }

        public RoutineExecutionModel CurrentRoutineInProgress
        {
            get
            {
                var routineInProgress = new Machine().SelectById<Machine>(Id).RoutineInProgress;

                if (routineInProgress == null) return null;

                return new RoutineExecutionModel { Routine = new RoutineModel { Name = routineInProgress.Routine.Name } };
            }
        }

        public string FullName { get; set; }
        public string HostName { get; set; }
        public MachineStatus? status;
        public MachineStatus Status
        {
            get
            {
                if (status != null)
                    return status.Value;

                var ipStatus = WindowsUtil.GetStatus(FullName);

                status = ipStatus == IPStatus.Success ? MachineStatus.Running : MachineStatus.Stopped;

                return status.Value;
            }

            set { }
        }

        public DateTime LastShutDown
        {
            get { return DateTime.MinValue; }// return Status == MachineStatus.Running ? WindowsUtil.GetLastSystemShutdown(FullName, NetworkUser, NetworkPassword) : DateTime.MinValue; }
        }

        public string Ram
        {
            get { return Status == MachineStatus.Running ? WindowsUtil.GetPhysicalMemory(FullName, NetworkUser, NetworkPassword) : "-"; }
        }

        public string DiskFreeSpace
        {
            get { return "-"; }// Status == MachineStatus.Running ? WindowsUtil.GetDiskFreeSpace(FullName, NetworkUser, NetworkPassword) : "-"; }
        }

        public string DiskSize
        {
            get { return "-"; }//Status == MachineStatus.Running ? WindowsUtil.GetDiskSize(FullName, NetworkUser, NetworkPassword) : "-"; }
        }

        public string ProcessorInformation
        {
            get { return "-"; }//Status == MachineStatus.Running ? WindowsUtil.GetProcessorInformation(FullName, NetworkUser, NetworkPassword) : "-"; }
        }

        public string OperatingSystemInformation
        {
            get { return Status == MachineStatus.Running ? WindowsUtil.GetOperatingSystemInformation(FullName, NetworkUser, NetworkPassword) : "-"; }
        }

        public List<string> InstalledApplications
        {
            get { return null; }//Status == MachineStatus.Running ? WindowsUtil.GetInstalledApplications(FullName, NetworkUser, NetworkPassword) : null; }
        }
    }
}